/*************************************************************************/
/*									 */
/*	Main routine, rdt						 */
/*	------------------						 */
/*									 */
/*************************************************************************/


#include "defns.i"
#include "types.i"


    /*  External data, described in extern.i  */

short		MaxAtt, MaxClass, MaxDiscrVal = 2;

ItemNo		MaxItem;

Description	*Item;

DiscrValue	*MaxAttVal;

char		*SpecialStatus;

String		*ClassName,
		*AttName,
		**AttValName,
		FileName = "DF";

short		VERBOSITY = 0,
		TRIALS    = 10;

Boolean		GAINRATIO  = true,
		SUBSET     = false,
		BATCH      = true,
		UNSEENS    = false,
		PROBTHRESH = false;

ItemNo		MINOBJS   = 2,
		WINDOW    = 0,
		INCREMENT = 0;

float		CF = 0.25;

Tree		*Pruned;

Boolean		AllKnown = true;

int             RDT_SEED = 2005;
float           DepthCounter; /*dynamically count tree depth when tree is growing*/


short           RDT_depth;     /* this actually the # of atts tested*/
                               /* it should be half of the (MaxAtt+1)*/
Tree            *Raw;

int **TestedConIndex;	

Boolean         FullTree=false;

    main(Argc, Argv)
/*  ----  */
    int Argc;
    char *Argv[];
{
    int o;
    extern char *optarg;
    extern int optind;
    Boolean FirstTime=true;
    short Best, BestTree();
    short t;

    PrintHeader("Random Decision Tree Generator");

    /*  Process options  */

    while ( (o = getopt(Argc, Argv, "f:bupv:t:w:i:gsm:c:")) != EOF )
    {
	if ( FirstTime )
	{
	    printf("\n    Options:\n");
	    FirstTime = false;
	}

	switch (o)
	{
	case 'f':   FileName = optarg;
		    printf("\tFile stem <%s>\n", FileName);
		    break;
	case 'b':   BATCH = true;
		    printf("\tWindowing disabled (now the default)\n");
		    break;
	case 'u':   UNSEENS = true;
		    printf("\tTrees evaluated on unseen cases\n");
		    break;
	case 'p':   PROBTHRESH = true;
		    printf("\tProbability thresholds used\n");
		    break;
	case 'v':   VERBOSITY = atoi(optarg);
		    printf("\tVerbosity level %d\n", VERBOSITY);
		    break;
	case 't':   TRIALS = atoi(optarg);
	            printf("\tBuilding %d random decision trees.....\n", TRIALS);
		    Check(TRIALS, 1, 10000);
		    BATCH = false;
		    break;
	case 'w':   WINDOW = atoi(optarg);
		    printf("\tInitial window size of %d items\n", WINDOW);
		    Check(WINDOW, 1, 1000000);
		    BATCH = false;
		    break;
       
		    /* initialize the seed*/
	case 'i':   RDT_SEED = atoi(optarg);
	            printf("\tBuilding random decision trees using SEED %d.....\n", RDT_SEED);
		    break;
		    
	            /* build full random tree or not*/
	case 'g':   FullTree = true;
		    printf("\tBuild Full Random Decision Tree\n");
		    break;

		    /**************************************************/
	case 's':   SUBSET = true;
		    printf("\tTests on discrete attribute groups\n");
		    break;
	case 'm':   MINOBJS = atoi(optarg);
		    printf("\tSensible test requires 2 branches with >=%d cases\n",
			    MINOBJS);
		    Check(MINOBJS, 1, 1000000);
		    break;
	case 'c':   CF = atof(optarg);
		    printf("\tPruning confidence level %g%%\n", CF);
		    Check(CF, Epsilon, 100);
		    CF /= 100;
		    break;
	case '?':   printf("unrecognised option\n");
		    exit(1);
	}
    }

    /*  Initialise  */

    GetNames();
    GetData(".data");
    printf("\nRead %d cases (%d attributes) from %s.data\n",
	   MaxItem+1, MaxAtt+1, FileName);
    RDT_depth = (short)ceil((MaxAtt+1.0)/2.0);

    Raw =(Tree*)calloc(TRIALS,sizeof(Tree));
   
    srand(RDT_SEED);
    
    ForEach(t, 0, TRIALS-1 )
	{
	    InitialiseTreeData();
	    InitialiseWeights();
	    AllKnown=true;
            Verbosity(2)
	    printf("\n--------\nRandom Decision Tree %d\n--------\n\n", t);
	    

	    DepthCounter=0;
	
	
	    Raw[t]= FormTree(0,MaxItem,DepthCounter);
	    
	    free(TestedConIndex);
	
            Verbosity(1)
            {
	    printf("\n");
	    PrintTree(Raw[t]);
	    }
	}	
    
    if(FullTree)
      printf("\n\nRandom Full Decision trees:  Evaluation on training data (%d items):\n", MaxItem+1);
    else
      printf("\n\nRandom Half Decision trees:  Evaluation on training data (%d items):\n", MaxItem+1);
    
    RDT_Evaluate(false);
    
    
    if ( UNSEENS )
        {
            GetData(".test");  
	    if(FullTree)
	      printf("\n\nRandom Full Decision trees:  Evaluation on test data (%d items):\n", MaxItem+1);
	    else
	      printf("\n\nRandom Half Decision trees:  Evaluation on test data (%d items):\n", MaxItem+1);
            
            RDT_Evaluate(true);
        }
            

    
    ForEach(t, 0, TRIALS-1 )
	ReleaseTree(Raw[t]);

    
    exit(0);
}

 
